#include <string>
#include <sstream>
#include <vector>
#include "json.h"

using namespace std;

string enemy_name = " ";

string get_enemy_name()
{
	return enemy_name;
}

int determine_our_team_id(json::Object game_state)
{
	// Initialize variables
	int our_team_id;

	// Search game state for our team name corresponding to our team id
	for (int i = 0; i < 2; i++)
	{
		string team_name = game_state["players"][i]["name"];
		if (team_name == "Chicken Wing Wednesdays")
		{
			our_team_id = i;
			break;
		}
	}
	return our_team_id;
}

int determine_enemy_team_id(json::Object game_state)
{
	// Initialize variables
	int enemy_team_id;

	// Search game state for enemy team name corresponding to enemy team id
	for (int i = 0; i < 2; i++)
	{
		string team_name = game_state["players"][i]["name"];
		if (team_name != "Chicken Wing Wednesdays")
		{
			enemy_team_id = i;
			break;
		}
	}
	return enemy_team_id;
}

void set_enemy_name(json::Object game_state)
{
	int team_id = determine_enemy_team_id(game_state);
	enemy_name = game_state["players"][team_id]["name"];
}

bool is_our_fast_tank_alive(json::Object game_state)
{
	// Initialize variables
	int our_team_id;
	bool alive = false;

	// Find team id
	our_team_id = determine_our_team_id(game_state);

	// Use team id to find fast tank status
	for (int i = 0; i < game_state["players"][our_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][our_team_id]["tanks"][i]["type"] == "TankFast")
		{
			alive = game_state["players"][our_team_id]["tanks"][i]["alive"];
			break;
		}
	}
	return alive;
}

bool is_our_slow_tank_alive(json::Object game_state)
{
	// Initialize variables
	int our_team_id;
	bool alive = false;

	// Find team id
	our_team_id = determine_our_team_id(game_state);

	// Use team id to find slow tank status
	for (int i = 0; i < game_state["players"][our_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][our_team_id]["tanks"][i]["type"] == "TankSlow")
		{
			alive = game_state["players"][our_team_id]["tanks"][i]["alive"];
			break;
		}
	}
	return alive;
}

bool is_enemy_fast_tank_alive(json::Object game_state)
{
	// Initialize variables
	int enemy_team_id;
	bool alive = false;

	// Find team id
	enemy_team_id = determine_enemy_team_id(game_state);

	// Use team id to find fast tank status
	for (int i = 0; i < game_state["players"][enemy_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][enemy_team_id]["tanks"][i]["type"] == "TankFast")
		{
			alive = game_state["players"][enemy_team_id]["tanks"][i]["alive"];
			break;
		}
	}
	return alive;
}

bool is_enemy_slow_tank_alive(json::Object game_state)
{
	// Initialize variables
	int enemy_team_id;
	bool alive = false;

	// Find team id
	enemy_team_id = determine_enemy_team_id(game_state);

	// Use team id to find slow tank status
	for (int i = 0; i < game_state["players"][enemy_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][enemy_team_id]["tanks"][i]["type"] == "TankSlow")
		{
			alive = game_state["players"][enemy_team_id]["tanks"][i]["alive"];
			break;
		}
	}
	return alive;
}

string determine_our_tank_id(json::Object game_state, string type)
{
	// Initialize variables
	int our_team_id;
	string our_tank_id;

	// Find team id
	our_team_id = determine_our_team_id(game_state);

	// Use our team id to find our tank id
	for (int i = 0; i < game_state["players"][our_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][our_team_id]["tanks"][i]["type"] == type)
		{
			our_tank_id = game_state["players"][our_team_id]["tanks"][i]["id"];
			break;
		}
	}
	return our_tank_id;
}

stringstream our_fast_tank_position(json::Object game_state)
{
	// Initialize variables
	stringstream position;
	int our_team_id;

	// Find team id
	our_team_id = determine_our_team_id(game_state);

	// Use team id to find fast tank position
	for (int i = 0; i < game_state["players"][our_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][our_team_id]["tanks"][i]["type"] == "TankFast")
		{
			int a = 0, b = 1;
			double x = game_state["players"][our_team_id]["tanks"][i]["position"][a];
			double y = game_state["players"][our_team_id]["tanks"][i]["position"][b];
			position << x << " " << y;
			break;
		}
	}
	return position;
}

stringstream enemy_fast_tank_position(json::Object game_state)
{
	// Initialize variables
	stringstream position;
	int enemy_team_id;

	// Find team id
	enemy_team_id = determine_enemy_team_id(game_state);

	// Use team id to find fast tank position
	for (int i = 0; i < game_state["players"][enemy_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][enemy_team_id]["tanks"][i]["type"] == "TankFast")
		{
			int a = 0, b = 1;
			double x = game_state["players"][enemy_team_id]["tanks"][i]["position"][a];
			double y = game_state["players"][enemy_team_id]["tanks"][i]["position"][b];
			position << x << " " << y;
			break;
		}
	}
	return position;
}

stringstream our_slow_tank_position(json::Object game_state)
{
	// Initialize variables
	stringstream position;
	int our_team_id;

	// Find team id
	our_team_id = determine_our_team_id(game_state);

	// Use team id to find slow tank position
	for (int i = 0; i < game_state["players"][our_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][our_team_id]["tanks"][i]["type"] == "TankSlow")
		{
			int a = 0, b = 1;
			double x = game_state["players"][our_team_id]["tanks"][i]["position"][a];
			double y = game_state["players"][our_team_id]["tanks"][i]["position"][b];
			position << x << " " << y;
			break;
		}
	}
	return position;
}

stringstream enemy_slow_tank_position(json::Object game_state)
{
	// Initialize variables
	stringstream position;
	int enemy_team_id;

	// Find team id
	enemy_team_id = determine_enemy_team_id(game_state);

	// Use team id to find slow tank position
	for (int i = 0; i < game_state["players"][enemy_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][enemy_team_id]["tanks"][i]["type"] == "TankSlow")
		{
			int a = 0, b = 1;
			double x = game_state["players"][enemy_team_id]["tanks"][i]["position"][a];
			double y = game_state["players"][enemy_team_id]["tanks"][i]["position"][b];
			position << x << " " << y;
			break;
		}
	}
	return position;
}

double our_fast_tank_direction(json::Object game_state)
{
	// Initialize variables
	int our_team_id;
	double direction;

	// Find team id
	our_team_id = determine_our_team_id(game_state);

	// Use team id to find fast tank direction
	for (int i = 0; i < game_state["players"][our_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][our_team_id]["tanks"][i]["type"] == "TankFast")
		{
			direction = game_state["players"][our_team_id]["tanks"][i]["tracks"];
			break;
		}
	}
	return direction / 3.14159265359 * 180;
}

double our_slow_tank_direction(json::Object game_state)
{
	// Initialize variables
	int our_team_id;
	double direction;

	// Find team id
	our_team_id = determine_our_team_id(game_state);

	// Use team id to find slow tank direction
	for (int i = 0; i < game_state["players"][our_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][our_team_id]["tanks"][i]["type"] == "TankSlow")
		{
			direction = game_state["players"][our_team_id]["tanks"][i]["tracks"];
			break;
		}
	}
	return direction / 3.14159265359 * 180;
}

double enemy_fast_tank_direction(json::Object game_state)
{
	// Initialize variables
	int enemy_team_id;
	double direction;

	// Find team id
	enemy_team_id = determine_enemy_team_id(game_state);

	// Use team id to find fast tank direction
	for (int i = 0; i < game_state["players"][enemy_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][enemy_team_id]["tanks"][i]["type"] == "TankFast")
		{
			direction = game_state["players"][enemy_team_id]["tanks"][i]["tracks"];
			break;
		}
	}
	return direction / 3.14159265359 * 180;
}

double enemy_slow_tank_direction(json::Object game_state)
{
	// Initialize variables
	int enemy_team_id;
	double direction;

	// Find team id
	enemy_team_id = determine_enemy_team_id(game_state);

	// Use team id to find slow tank direction
	for (int i = 0; i < game_state["players"][enemy_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][enemy_team_id]["tanks"][i]["type"] == "TankSlow")
		{
			direction = game_state["players"][enemy_team_id]["tanks"][i]["tracks"];
			break;
		}
	}
	return direction / 3.14159265359 * 180;
}

double our_fast_tank_turret_direction(json::Object game_state)
{
	// Initialize variables
	int our_team_id;
	double direction;

	// Find team id
	our_team_id = determine_our_team_id(game_state);

	// Use team id to find fast tank turret direction
	for (int i = 0; i < game_state["players"][our_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][our_team_id]["tanks"][i]["type"] == "TankFast")
		{
			direction = game_state["players"][our_team_id]["tanks"][i]["turret"];
			break;
		}
	}
	return direction / 3.14159265359 * 180;
}

double our_slow_tank_turret_direction(json::Object game_state)
{
	// Initialize variables
	int our_team_id;
	double direction;

	// Find team id
	our_team_id = determine_our_team_id(game_state);

	// Use team id to find slow tank turret direction
	for (int i = 0; i < game_state["players"][our_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][our_team_id]["tanks"][i]["type"] == "TankSlow")
		{
			direction = game_state["players"][our_team_id]["tanks"][i]["turret"];
			break;
		}
	}
	return direction / 3.14159265359 * 180;
}

double enemy_fast_tank_turret_direction(json::Object game_state)
{
	// Initialize variables
	int enemy_team_id;
	double direction;

	// Find team id
	enemy_team_id = determine_enemy_team_id(game_state);

	// Use team id to find fast tank turret direction
	for (int i = 0; i < game_state["players"][enemy_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][enemy_team_id]["tanks"][i]["type"] == "TankFast")
		{
			direction = game_state["players"][enemy_team_id]["tanks"][i]["turret"];
			break;
		}
	}
	return direction / 3.14159265359 * 180;
}

double enemy_slow_tank_turret_direction(json::Object game_state)
{
	// Initialize variables
	int enemy_team_id;
	double direction;

	// Find team id
	enemy_team_id = determine_enemy_team_id(game_state);

	// Use team id to find slow tank turret direction
	for (int i = 0; i < game_state["players"][enemy_team_id]["tanks"].size(); i++)
	{
		if (game_state["players"][enemy_team_id]["tanks"][i]["type"] == "TankSlow")
		{
			direction = game_state["players"][enemy_team_id]["tanks"][i]["turret"];
			break;
		}
	}
	return direction / 3.14159265359 * 180;
}

vector<vector<double>> all_projectiles(json::Object game_state)
{
	// Variables
	vector<vector<double>> list;

	// Put all projectile data into the container
	for (int j = 0; j < 2; j++)
	{
		json::Array l = game_state["players"][j]["tanks"];
		for (int i = 0; i < l.size(); i++)
		{
			for (int n = 0; n < l[i]["projectiles"].size(); n++)
			{
				vector<double> k;
				double x = l[i]["projectiles"][n]["position"][(size_t)0];
				double y = l[i]["projectiles"][n]["position"][(size_t)1];
				double direction = l[i]["projectiles"][n]["direction"];
				k.push_back(x);
				k.push_back(y);
				k.push_back(direction);
				list.push_back(k);
			}
		}
	}
	return list;
}